//
//  SaveListVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/1/4.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "SaveListVC.h"
#import "SaveListCell.h"
#import "RailTransportDao.h"
#import "BinningCarVC.h"
#import "BinningBracketVC.h"
#import "TrainShiftModel.h"

@interface SaveListVC ()
// 列表数据
@property (strong ,nonatomic)NSMutableArray *listDataArray;

@end

@implementation SaveListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 设置数据
    [self setData];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"保存列表";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[SaveListCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[SaveListCell reuseIdentifier]];
}

// 设置数据
- (void)setData
{
    // 用户ID 测试暂时写101
    // 根据数据类型进行判断
    switch (_dataType) {
        case 0:{
            // 查询西向装车数据
            _listDataArray = [RailTransportDao selectBinningWest:TestUserId];
            break;
        }
        case 1:{
            // 查询东向装车数据
            _listDataArray = [RailTransportDao selectBinningEast:TestUserId];
            break;
        }
        case 2:{
            // 查询支架或托盘装车数据
            _listDataArray = [RailTransportDao selectBinningBracket:TestUserId];
            break;
        }
        default:
            break;
    }
    // 防止数据查询速度慢不能及时更新数据
    [_myTableView reloadData];
}

// 删除数据
- (void)deleteData:(NSIndexPath *)indexPath
{
    // 根据数据类型进行判断
    switch (_dataType) {
        case 0:{
            BinningWestModel *binningWestModel = _listDataArray[indexPath.row];
            [self stateShowMessage:[RailTransportDao deleteBinningWest:binningWestModel.westId]];
            break;
        }
        case 1:{
            BinningEastModel *binningEastModel = _listDataArray[indexPath.row];
            [self stateShowMessage:[RailTransportDao deleteBinningEast:binningEastModel.eastId]];
            break;
        }
        case 2:{
            BinningBracketModel *binningBracketModel = _listDataArray[indexPath.row];
            [self stateShowMessage:[RailTransportDao deleteBinningBracket:binningBracketModel.bracketId]];
            break;
        }
        default:
            break;
    }
}
// 根据状态显示信息提示语
- (void)stateShowMessage:(BOOL)state
{
    // 根据状态提示
    if (state) {
        [MBProgressHUD showSuccess:@"删除成功"];
    }else{
        [MBProgressHUD showError:@"删除失败"];
    }
}
// 创建trainShiftModel
- (TrainShiftModel *)creatTrainShiftModel:(NSString *)departDirection departDirectionType:(NSString *)departDirectionType date:(NSString *)date location:(NSString *)location supplier:(NSString *)supplier
{
    TrainShiftModel *trainShiftModel = [[TrainShiftModel alloc]init];
    trainShiftModel.departDirection = departDirection;
    trainShiftModel.departDirectionType = departDirectionType;
    trainShiftModel.date = date;
    trainShiftModel.location = location;
    trainShiftModel.supplier = supplier;
    return trainShiftModel;
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SaveListCell *cell = [tableView dequeueReusableCellWithIdentifier:[SaveListCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    [cell setData:_listDataArray[indexPath.row]];
    return cell;
}
#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据数据类型进行判断
    switch (_dataType) {
        case 0:{
            // 西向装车
            BinningCarVC *binningCarVC = [[BinningCarVC alloc]init];
            BinningWestModel *binningWestModel = _listDataArray[indexPath.row];
            // 暂时暂定西向为1  东向为2
            binningCarVC.trainShiftModel = [self creatTrainShiftModel:binningWestModel.directionNo departDirectionType:@"1" date:binningWestModel.date location:binningWestModel.location supplier:binningWestModel.supplier];
            binningCarVC.binningWestModel = binningWestModel;
            [self.navigationController pushViewController:binningCarVC animated:YES];
            break;
        }
        case 1:{
            // 东向装车
            BinningCarVC *binningCarVC = [[BinningCarVC alloc]init];
            BinningEastModel *binningEastModel = _listDataArray[indexPath.row];
            // 暂时暂定西向为1  东向为2
            binningCarVC.trainShiftModel = [self creatTrainShiftModel:binningEastModel.directionNo departDirectionType:@"2" date:binningEastModel.date location:binningEastModel.location supplier:binningEastModel.supplier];
            binningCarVC.binningEastModel = binningEastModel;
            [self.navigationController pushViewController:binningCarVC animated:YES];
            break;
        }
        case 2:{
            // 装支架
            BinningBracketVC *binningBracketVC = [[BinningBracketVC alloc]init];
            BinningBracketModel *binningBracketModel = _listDataArray[indexPath.row];
            // 暂时暂定西向为1  东向为2
            binningBracketVC.trainShiftModel = [self creatTrainShiftModel:binningBracketModel.directionNo departDirectionType:@"" date:binningBracketModel.date location:binningBracketModel.location supplier:binningBracketModel.supplier];
            binningBracketVC.binningBracketModel = binningBracketModel;
            [self.navigationController pushViewController:binningBracketVC animated:YES];
            break;
        }
        default:
            break;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 移除数据库数据
        [self deleteData:indexPath];
        // 移除数据
        [_listDataArray removeObjectAtIndex:indexPath.row];
        // 移除单元格
        //        [_myTableView reloadData];
        [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// 设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
